package {
	import com.greensock.TweenMax;
	import com.greensock.plugins.VisiblePlugin;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import org.osflash.signals.Signal;
	
	import subumbo.leg.model.Lib;
	
	public final class MenuCircle extends Circle {
		
		public var play:Signal; 		
		public var explore:Signal;
		public var showSignal:Signal;
		public var resume:Signal;
		private var _menu:Sprite;
		private var _thirdYPos:Number;
		private var _resumeItem:MovieClip;
		private var _giftItem:MovieClip;
		
		public function MenuCircle(view:MovieClip) {
			super(view);
			play = new Signal();
			explore = new Signal();
			showSignal = new Signal();
			resume = new Signal();
			buildMenu();
			_view.arrow.visible = false;
			_view.arrow.alpha = 0;
			_view.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
			_view.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
		}
		
		private function onStart(e:MouseEvent):void {
			_view.buttonMode = false;
			_view.mouseChildren = true;
			_view.removeEventListener(MouseEvent.MOUSE_DOWN, onStart);
			TweenMax.to(_view.logo, .5, {y:_view.logo.y -60, scaleX:.6, scaleY:.6});
			showMenu();
		}
		
		
		private function buildMenu():void {
			_menu = new Sprite();
			_view.addChild(_menu);
			
			var y:Number;
			
			var item:MovieClip = createButton("New Game");
			item.addEventListener(MouseEvent.MOUSE_DOWN, onPlay, false, 0 , true);
			_menu.addChild(item);
			item.y = y;
			
			y = item.height + item.y;
			
			item = createButton("Resume");
			item.addEventListener(MouseEvent.MOUSE_DOWN, onResume, false, 0 ,true);
			item.y = y;
			item.visible =false;
			_resumeItem = item;
			_menu.addChild(item);
			
			_thirdYPos = item.height + item.y;
			
			item = createButton("Gift Shop");
			item.addEventListener(MouseEvent.MOUSE_DOWN, onGiftShop, false, 0 , true);
			_menu.addChild(item);
			item.y = y;
			_giftItem = item;
			
			_menu.visible = false;
			_menu.alpha = 0;
		}
		
		public function showResume(val:Boolean):void {
			if(val) {
				_giftItem.y = _thirdYPos;
				_resumeItem.visible = true;
			}else {
				_giftItem.y = _resumeItem.y;
				_resumeItem.visible = false;
			}
		}
		
		private function onResume(e:MouseEvent):void {
			resume.dispatch();
			hide();
		}
		
		private function showMenu():void {
			_menu.visible = true;
			TweenMax.to(_menu, 1, {alpha:1});
		}
		
		private function hideMenu():void {
			_menu.visible = true;
			TweenMax.to(_menu, 1, {alpha:0, onComplete:function():void { _menu.visible = false; }});
		}
		
		private function onExplore(e:MouseEvent):void {
			explore.dispatch();	
			hide();
		} 
			
		private function onPlay(e:MouseEvent):void {
			play.dispatch();
			hide();
		}
		
		private function onHighScores(e:MouseEvent):void {
			
		}
		
		private function onGiftShop(e:MouseEvent):void {
			navigateToURL(new URLRequest("http://society6.com/TheEyesHaveIt"), "_blank");
		}
		
		protected override function onRollOver(e:MouseEvent):void {
			TweenMax.to(_view, .5, {scaleX:1, scaleY:1, y:_view.stage.stageHeight*.5});
			_view.arrow.visible = false;
			_view.arrow.alpha = 1;
			showMenu();
			showSignal.dispatch();
		} 
		
		private function hide():void {
			TweenMax.to(_view, .4, {scaleX:.3, scaleY:.3, y:-15});
			_view.arrow.visible = true;
			TweenMax.to(_view.arrow, .4, {alpha:1, delay:.5, onComplete:function():void { view.addEventListener(MouseEvent.ROLL_OVER, onRollOver, false, 0, true); }});
			hideMenu();
		}
		
		private function createButton(label:String):MovieClip {
			var m:MovieClip = Lib.getMovieClip("MenuButton");
			m.addEventListener(MouseEvent.ROLL_OVER, onMenuButton);
			m.addEventListener(MouseEvent.ROLL_OUT, onMenuButton);
			m.buttonMode = true;
			m.mouseChildren = false;
			m.txt.autoSize = "center";
			m.txt.text = label;
			return m;
		}
		
		private function onMenuButton(e:MouseEvent):void {
			switch(e.type) {
				case MouseEvent.ROLL_OVER :
					TweenMax.to(e.currentTarget, .2, {tint:0xFFFFFF, scaleX:1.1, scaleY:1.1});
					break;
				
				case MouseEvent.ROLL_OUT :
					TweenMax.to(e.currentTarget, .2, {tint:0x000000, scaleX:1, scaleY:1});
					break;
			}
		}
		
		public function show():void {
			onRollOver(null);
		}
	}
}